//
//  MyPublishVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MyPublishVC.h"
#import "ModelMyPublish.h"
#import "MyAcceptMovieListVC.h"
#import "MyPublishCell.h"
#import "PublishDateVC.h"
@interface MyPublishVC ()<MyPublishCellDelegate>
{
    int pIntPage;
}

@property(nonatomic, strong) NSMutableArray *pDataDicArr;
@property(nonatomic, strong) NSMutableArray *pEditDicArr; //编辑发布 使用这个数组
@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation MyPublishVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.pTableView.mj_header beginRefreshing];
}

- (void)initInfo
{
    [super initInfo];
    
    pIntPage = 1;
    _pDataDicArr = [NSMutableArray array];
    _pEditDicArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"我的发布"];
    
    [self.pTableView setTableFooterView:[UIView new]];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  
        pIntPage = 1;
        [weakSelf requestDataDicDowm];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_footer setHidden:YES];
}

- (void)requestDataDic
{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestMyReleaseMovieList:MY_Release_MovieList page:pIntPage pageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"movieList"];
            NSLog(@"附近用户信息： %@", dictionary);
            for (NSDictionary *dic in arr)
            {
                ModelMyPublish *myPublish = [[ModelMyPublish alloc] init];
                
                myPublish.acAgree       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"acAgree"]];
                myPublish.appId         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appId"]];
                myPublish.jd            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"jd"]];
                myPublish.meetAddress   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"meetAddress"]];
                myPublish.meetPersonNum = [NSString stringWithFormat:@"%@",[dic valueForKey:@"meetPersonNum"]];
                myPublish.meetTime      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"meetTime"]];
                myPublish.moiveImage    = [NSString stringWithFormat:@"%@",[dic valueForKey:@"moiveImage"]];
                myPublish.movieId       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieId"]];
                myPublish.needVerified  = [NSString stringWithFormat:@"%@",[dic valueForKey:@"needVerified"]];
                myPublish.payType       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"payType"]];
                myPublish.phone         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                myPublish.plan          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"plan"]];
                myPublish.releaseAddress= [NSString stringWithFormat:@"%@",[dic valueForKey:@"releaseAddress"]];
                myPublish.releaseTime   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"releaseTime"]];
                myPublish.status        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"status"]];
                myPublish.takePeople    = [NSString stringWithFormat:@"%@",[dic valueForKey:@"takePeople"]];
                myPublish.title         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"title"]];
                myPublish.uid           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                myPublish.wd            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"wd"]];
                
                [_pDataDicArr addObject:myPublish];
                 [_pEditDicArr addObject:dic];
            }
        
            [weakSelf.pTableView reloadData];
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
            
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }

    }];
}

- (void)requestDataDicDowm
{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestMyReleaseMovieList:MY_Release_MovieList page:pIntPage pageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [weakSelf.pDataDicArr removeAllObjects];
            [weakSelf.pEditDicArr removeAllObjects];
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"movieList"];
            NSLog(@"附近用户信息： %@", dictionary);
            for (NSDictionary *dic in arr)
            {
                ModelMyPublish *myPublish = [[ModelMyPublish alloc] init];
                
                myPublish.acAgree       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"acAgree"]];
                myPublish.appId         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appId"]];
                myPublish.jd            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"jd"]];
                myPublish.meetAddress   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"meetAddress"]];
                myPublish.meetPersonNum = [NSString stringWithFormat:@"%@",[dic valueForKey:@"meetPersonNum"]];
                myPublish.meetTime      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"meetTime"]];
                myPublish.moiveImage    = [NSString stringWithFormat:@"%@",[dic valueForKey:@"moiveImage"]];
                myPublish.movieId       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieId"]];
                myPublish.needVerified  = [NSString stringWithFormat:@"%@",[dic valueForKey:@"needVerified"]];
                myPublish.payType       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"payType"]];
                myPublish.phone         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                myPublish.plan          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"plan"]];
                myPublish.releaseAddress= [NSString stringWithFormat:@"%@",[dic valueForKey:@"releaseAddress"]];
                myPublish.releaseTime   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"releaseTime"]];
                myPublish.status        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"status"]];
                myPublish.takePeople    = [NSString stringWithFormat:@"%@",[dic valueForKey:@"takePeople"]];
                myPublish.title         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"title"]];
                myPublish.uid           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                myPublish.wd            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"wd"]];
                
                [_pDataDicArr addObject:myPublish];
                [_pEditDicArr addObject:dic];
            }
    
            [weakSelf.pTableView reloadData];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
            
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
        
        [self.pTableView.mj_footer endRefreshing];
        [self.pTableView.mj_header endRefreshing];

    }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _pDataDicArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   MyPublishCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfMyPublishCell"];
    
    if (!cell) {
        cell = [[MyPublishCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"idfMyPublishCell"];
    }
    ModelMyPublish *myPublish = [_pDataDicArr objectAtIndex:indexPath.section];

    UIImageView *imgPic = (UIImageView *)[cell.contentView viewWithTag:1301];
    UIImageView *imgState = (UIImageView *)[cell.contentView viewWithTag:1302];
    UILabel *lblTitle = (UILabel *)[cell.contentView viewWithTag:1101];
    UILabel *lblPublishTime = (UILabel *)[cell.contentView viewWithTag:1102];
    UILabel *lblStartTime = (UILabel *)[cell.contentView viewWithTag:1103];
    UILabel *lblAddress = (UILabel *)[cell.contentView viewWithTag:1104];
    UILabel *lblNum = (UILabel *)[cell.contentView viewWithTag:1105];

    [imgPic sd_setImageWithURL:[NSURL URLWithString:myPublish.moiveImage]];
    [imgState setImage:[UIImage imageNamed:[myPublish.payType intValue]==1?@"treat_me":([myPublish.payType intValue]==2?@"treat_you":@"treat_AA")]];
    [lblTitle setText:[NSString stringWithFormat:@"标题:%@", myPublish.title]];
    [lblPublishTime setText:[NSString stringWithFormat:@"发布时间:%@", myPublish.releaseTime.length>=16?[myPublish.releaseTime substringToIndex:16]:myPublish.releaseTime]];
    [lblStartTime setText:myPublish.meetTime];
    [lblAddress setText:myPublish.meetAddress.length==0?[User defaultUser].pStrLocationAddress:myPublish.meetAddress];
    [lblStartTime setText:myPublish.meetTime];
    if ([myPublish.acAgree intValue] == 0) {
        [lblNum setText:[NSString stringWithFormat:@"%@人赴约", myPublish.meetPersonNum]];
    } else {
        [lblNum setText:@"已生成约会"];
    }
    if ([myPublish.meetPersonNum intValue] == 0 || [myPublish.acAgree intValue] != 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   
    cell.dataDic = [_pEditDicArr objectAtIndex:indexPath.section];
    NSDictionary *dic = [_pEditDicArr objectAtIndex:indexPath.section];
    cell.dataDic = dic;
    cell.delegate =self;
//    cell.pBtnDelete.appId = [dic valueForKey:@"appId"];
//    [cell.pBtnDelete addTarget:self action:@selector(deletePublishAction:) forControlEvents:(UIControlEventTouchUpInside)];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelMyPublish *myPublish = [_pDataDicArr objectAtIndex:indexPath.section];
  //  if ([myPublish.meetPersonNum intValue] > 0 && [myPublish.acAgree intValue] == 0) {
        [self performSegueWithIdentifier:@"idfMyAcceptMovieListVC" sender:myPublish];
//}
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark MyPublishCellDelegate

/*
 @property(nonatomic,strong)NSString *  movieId;
 @property(nonatomic,strong)NSString *  moiveImage;
 @property(nonatomic,strong)NSString *  moiveTitle;
 @property(nonatomic,strong)NSString *  moiveScore;
 @property(nonatomic,strong)NSString *  moiveTime;
 @property(nonatomic,strong)NSString *  moiveType;
 @property(nonatomic,strong)NSString *  appNum;
 @property(nonatomic,strong)NSString *  introduction;
 @property(nonatomic,strong)NSString *  appId;
 @property(nonatomic,strong)NSString *  appointmentCount;
 @property(nonatomic,strong)NSString *  releaseTime;
 @property(nonatomic,strong)NSString *  countries;
 @property(nonatomic,strong)NSString *  placeOfIssue;
 @property(nonatomic,strong)NSString *  director;
 */
//点击编辑的时候触发
-(void)pushNextVC:(NSDictionary *)dic{
    UIStoryboard *seeStoyBoard = [UIStoryboard storyboardWithName:@"See" bundle:nil];
    
    PublishDateVC  *publishDateVC = [seeStoyBoard instantiateViewControllerWithIdentifier:@"idPublishDateVC"];
    Movie *m = [[Movie alloc]init];
    m.moiveImage = [dic valueForKey:@"moiveImage"];
    m.moiveTitle = [dic valueForKey:@"title"];
    m.releaseTime =[[[NSString stringWithFormat:@"%@",[dic valueForKey:@"releaseTime"]] componentsSeparatedByString:@"/"] firstObject];
    m.appNum = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appNum"]];
    m.moiveScore =  [NSString stringWithFormat:@"%@",  [dic valueForKey:@"moiveScore"]];
    m.appId =  [NSString stringWithFormat:@"%@", [dic valueForKey:@"appId"]];
    
    m.introduction = [NSString stringWithFormat:@"%@",[dic valueForKey:@"introduction"]];
    m.appointmentCount = [NSString stringWithFormat:@"%@个影约",[dic valueForKey:@"appointmentCount"]];
    m.moiveTime = [[[NSString stringWithFormat:@"%@",[dic valueForKey:@"meetTime"]] componentsSeparatedByString:@"("] firstObject];
    m.moiveType = [NSString stringWithFormat:@"%@",[dic valueForKey:@"payType"]];
    
    m.movieId = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieId"]];
    m.director = [NSString stringWithFormat:@"%@",[dic valueForKey:@"director"]];
    m.countries = [NSString stringWithFormat:@"%@",[dic valueForKey:@"countries"]];
    m.placeOfIssue = [NSString stringWithFormat:@"%@",[dic valueForKey:@"placeOfIssue"]];
    publishDateVC.movie = m;
    publishDateVC.movie_DateSate = em_edit_movie_Date;
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[[dic valueForKey:@"meetTime"] isEqualToString:@"周末"]?@"0":@"1", @"time", [NSString stringWithFormat:@"%@", [dic valueForKey:@"payType"]], @"type", nil];
    publishDateVC.dictEditInfo = dict;
    [self.navigationController pushViewController:publishDateVC animated:YES];
}
//点击删除的时候触发
-(void)deletePublishAction:(NSDictionary *)sender
{
    [SVProgressHUD show];
    [TDNetwork setMovieStatusURl:MY_deleteRelease objId:[sender valueForKey:@"appId"] status:@"0" complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
      if (succeed) {
          [SVProgressHUD showSuccessWithStatus:@"删除成功"];
          [self.pTableView.mj_header beginRefreshing];
      } else {
          [SVProgressHUD showErrorWithStatus:@"删除失败"];
      }
    }];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfMyAcceptMovieListVC"]) {
        MyAcceptMovieListVC *nextVC = segue.destinationViewController;
        nextVC.pModelMyPublish = sender;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
